package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IDriverInfoService;
import com.mdd.admin.validate.DriverInfoCreateValidate;
import com.mdd.admin.validate.DriverInfoUpdateValidate;
import com.mdd.admin.validate.DriverInfoSearchValidate;
import com.mdd.admin.vo.DriverInfoVo;
import com.mdd.admin.vo.DriverInfoDetailVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.user.DriverInfo;
import com.mdd.common.mapper.user.DriverInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 驾驶员信息实现类
 * @author LikeAdmin
 */
@Service
public class DriverInfoServiceImpl implements IDriverInfoService {

    @Resource
    DriverInfoMapper umsDriverInfoMapper;

    /**
     * 驾驶员信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<UmsDriverInfoListedVo>
     */
    @Override
    public PageResult<DriverInfoVo> list(PageValidate pageValidate, DriverInfoSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DriverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        umsDriverInfoMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userNo@user_no:str",
            "=:idCard@id_card:str",
            "=:idValidityTime@id_validity_time:str",
            "=:frontUrl@front_url:str",
            "=:oppositeUrl@opposite_url:str",
            "=:driveType@drive_type:long",
            "=:driveValidityTime@drive_validity_time:str",
            "=:driveUrl@drive_url:str",
            "=:issuedDate@issued_date:str",
        });

        IPage<DriverInfo> iPage = umsDriverInfoMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DriverInfoVo> list = new LinkedList<>();
        for(DriverInfo item : iPage.getRecords()) {
            DriverInfoVo vo = new DriverInfoVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 驾驶员信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return UmsDriverInfo
     */
    @Override
    public DriverInfoDetailVo detail(Long id) {
        DriverInfo model = umsDriverInfoMapper.selectOne(
                new QueryWrapper<DriverInfo>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "驾驶员信息详情数据不存在");

        DriverInfoDetailVo vo = new DriverInfoDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 驾驶员信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(DriverInfoCreateValidate createValidate) {
        DriverInfo model = new DriverInfo();
        model.setUserNo(createValidate.getUserNo());
        model.setIdCard(createValidate.getIdCard());
        model.setIdValidityTime(createValidate.getIdValidityTime());
        model.setFrontUrl(createValidate.getFrontUrl());
        model.setOppositeUrl(createValidate.getOppositeUrl());
        model.setDriveType(createValidate.getDriveType());
        model.setDriveValidityTime(createValidate.getDriveValidityTime());
        model.setDriveUrl(createValidate.getDriveUrl());
        model.setIssuedDate(createValidate.getIssuedDate());
        umsDriverInfoMapper.insert(model);
    }

    /**
     * 驾驶员信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(DriverInfoUpdateValidate updateValidate) {
        DriverInfo model = umsDriverInfoMapper.selectOne(
                new QueryWrapper<DriverInfo>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserNo(updateValidate.getUserNo());
        model.setIdCard(updateValidate.getIdCard());
        model.setIdValidityTime(updateValidate.getIdValidityTime());
        model.setFrontUrl(updateValidate.getFrontUrl());
        model.setOppositeUrl(updateValidate.getOppositeUrl());
        model.setDriveType(updateValidate.getDriveType());
        model.setDriveValidityTime(updateValidate.getDriveValidityTime());
        model.setDriveUrl(updateValidate.getDriveUrl());
        model.setIssuedDate(updateValidate.getIssuedDate());
        umsDriverInfoMapper.updateById(model);
    }

    /**
     * 驾驶员信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Long id) {
        DriverInfo model = umsDriverInfoMapper.selectOne(
                new QueryWrapper<DriverInfo>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        umsDriverInfoMapper.delete(new QueryWrapper<DriverInfo>().eq("id", id));
    }

}
